package com.slowly.config;

import com.jfinal.config.*;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.slowly.action.AdminController;
import com.slowly.action.IndexController;
import com.slowly.intercepto.ExceptionInterceptor;
import com.slowly.intercepto.LoginInterceptor;
import com.slowly.model.*;
import org.beetl.ext.jfinal3.JFinal3BeetlRenderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SysConfig extends JFinalConfig {
	private static Logger log = LoggerFactory.getLogger(SysConfig.class);
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		loadPropertyFile("init.properties");
		me.setDevMode(getPropertyToBoolean("devMode", true));
		JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
		rf.config();
		me.setRenderFactory(rf);
		me.setJsonFactory(new FastJsonFactory());
		me.setError401View("/WEB-INF/view/common/401.html");
		me.setError403View("/WEB-INF/view/common/403.html");
		me.setError404View("/WEB-INF/view/common/404.html");
		me.setError500View("/WEB-INF/view/common/500.html");
	}
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		log.info("configRoute 路由注册");
		me.setBaseViewPath("/WEB-INF/view");
		me.add("/", IndexController.class);
		me.add("/admin", AdminController.class);
	}
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("mysql.jdbcUrl"), getProperty("mysql.userName"),
				getProperty("mysql.passWord"), "com.mysql.jdbc.Driver");
		druidPlugin.setFilters("wall");
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setDevMode(true); // 设置开发模式
		arp.setShowSql(true); // 是否显示SQL
		arp.setDialect(new MysqlDialect());
		
		arp.addMapping("slow_languages", "langId", SlowLanguages.class);
		arp.addMapping("slow_message", "msgId", SlowMessage.class);
		arp.addMapping("slow_stamp", "stampId", SlowStamp.class);
		arp.addMapping("slow_user", "userId", SlowUser.class);
		arp.addMapping("slow_user_interest", "interestId", SlowUserInterest.class);
		arp.addMapping("slow_user_languages", "langId,userId", SlowUserLanguages.class);
		arp.addMapping("slow_user_stamp", "stampId,userId", SlowUserStamp.class);
		me.add(arp);
	}
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new LoginInterceptor());
		me.addGlobalActionInterceptor(new ExceptionInterceptor());
		// 配置开启事物规则
		me.add(new TxByMethods("save*", "update*", "delete*", "add*"));
	}
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
	}
	public void afterJFinalStart() {
	}
	public void configEngine(Engine me) {
	}
	public static void main(String[] args) {
		Prop prop = PropKit.use("init.properties");
		DruidPlugin druidPlugin = new DruidPlugin(prop.get("mysql.jdbcUrl"), prop.get("mysql.userName"),
				prop.get("mysql.passWord"), "com.mysql.jdbc.Driver");
		druidPlugin.setFilters("wall");
		druidPlugin.start();
		// base model 所使用的包名
		String baseModelPackageName = "com.slow.model.base";
		// base model 文件保存路径
		String baseModelOutputDir = PathKit.getWebRootPath() + "/src/com/slow/model/base";
		// model 所使用的包名 (MappingKit 默认使用的包名)
		String modelPackageName = "com.slow.model";
		// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
		String modelOutputDir = baseModelOutputDir + "/..";
		Generator gernerator = new Generator(druidPlugin.getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
		
		gernerator.generate();
	}
}
